﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private string _excel_file = "";
        private double _baseVal;
        private double _rateVal;
        private double _xiangVal;
        private double _loopVal;

        public Form1()
        {
            InitializeComponent();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        /// <summary>
        /// 打开文本框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "C:\\Users\\Administrator\\Documents\\Tencent Files\\1054619306\\FileRecv";
                openFileDialog.Filter = "All files (*.*)|*.*|xlsx files (*.xlsx)|*.xlsx";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //Get the path of specified file
                    filePath = openFileDialog.FileName;

                    //Read the contents of the file into a stream
                    var fileStream = openFileDialog.OpenFile();

                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        fileContent = reader.ReadToEnd();
                    }
                }
            }

            _excel_file = filePath;
            MessageBox.Show(filePath);
            
            //MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
        }

        private bool CheckVal()
        {
            string baseTxt = baseText.Text;
            double baseVal = 0f;
            if (!double.TryParse(baseTxt, out baseVal))
            {
                MessageBox.Show("基础数值不合法");
                return false;
            }
            _baseVal = baseVal;

            if (!double.TryParse(xiangTxt.Text, out _xiangVal))
            {
                MessageBox.Show("项数数值不合法");
                return false;
            }

            if (!double.TryParse(loopTxt.Text, out _loopVal))
            {
                MessageBox.Show("循环数值不合法");
                return false;
            }

            string rateTxt = rateText.Text;
            double rateVal = 0f;
            if (!double.TryParse(rateTxt, out rateVal))
            {
                MessageBox.Show("系数不合法");
                return false;
            }
            _rateVal = rateVal;

            if (string.IsNullOrEmpty(spanText.Text))
            {
                MessageBox.Show("请输入跨度公式");
                return false;
            }

            return true;
        }

        private void compute_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_excel_file))
            {
                MessageBox.Show("请先选择一个excel");
                return;
            }

            if (CheckVal())
            {
                _compute();
            }

        }

        private void _compute()
        {
            //替换公式
            string formula = spanText.Text;
            double K = _baseVal, F0 = _xiangVal, Fi = _rateVal;
            formula = formula.Replace("a", "{0}");
            formula = formula.Replace("b", "{1}");
            formula = formula.Replace("c", "{2}");
            formula = string.Format(formula, K, F0, Fi);
            DataTable dt = new DataTable();
            string really_data = dt.Compute(formula, "false").ToString();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < _loopVal; i++)
            {
                sb.AppendFormat("{0}\n", really_data);
            }
            outBox.Text = sb.ToString();
        }

        private void compute_once_Click(object sender, EventArgs e)
        {
            if (CheckVal())
            {
                _compute();
            }
        }
    }
}
// a + (1 + 2 * (b - 1) * 2 * (b - 1)) * c
